package com.syuswe.qimo_02.controller;

import com.syuswe.qimo_02.pojo.Dept;
import com.syuswe.qimo_02.pojo.Result;
import com.syuswe.qimo_02.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author n7c.swe
 * {@code @date} 2024-05
 */

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    private final DeptService deptService;
    @Autowired
    public DeptController(DeptService deptService){
        this.deptService = deptService;
    }

    @GetMapping
    public Result list() {

        List<Dept> deptList = deptService.list();
        log.info("查询全部部门数据");
        return Result.success(deptList);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) { // @PathVariable注解用于获取路径参数

        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) { // @RequestBody注解用于获取请求体中的数据json

        log.info("新增部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) { // @RequestBody注解用于获取请求体中的数据json

        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
